package com.training.pet;

import java.util.Arrays;
import java.util.Scanner;

public class PetSystem {

	public static void main(String[] args) {
		// 预设用户名密码
		final String USERNAME = "admin";
		final String PASSWORD = "123";

		int seq = 0;

		String pets[][] = new String[5][6];// 存储宠物信息
		Scanner sc = new Scanner(System.in);

		int count = 0;// 记录登录错误次数
		int index = 0;//记录位置
		while (count < 3) {
			// 用户登录界面
			System.out.println("-----------------------------");
			System.out.println("\t简易宠物信息管理系统");
			System.out.println("-----------------------------");

			System.out.println("请输入用户名：");
			String username = sc.nextLine();
			System.out.println("请输入密码：");
			String password = sc.nextLine();

			if (USERNAME.equals(username) && PASSWORD.equals(password)) {// 信息正确
				System.out.println("登录成功！");
				boolean isContinue = true;
				while (isContinue) {
					System.out.println("\t欢迎使用宠物信息管理系统");
					System.out.println("-----------------------------");
					System.out.println("1.添加宠物信息\t2.删除宠物信息");
					System.out.println("3.修改宠物信息\t4.显示所有宠物信息");
					System.out.println("0.退出");
					System.out.println("请选择：");
					String opt = sc.nextLine();
					switch (opt) {
					case "1":
						if(index+1>pets.length) {
							pets = Arrays.copyOf(pets, pets.length*2);
						}
						
						System.out.println("类型：");
						String category = sc.nextLine();

						System.out.println("品种：");
						String kind = sc.nextLine();

						boolean checkPrice = true;
						double price = 0.0;
						while (checkPrice) {
							System.out.println("价格：");
							String s = sc.nextLine();
							if (!s.matches("[0-9.]+")) {
								System.out.println("输入不合法,请重新输入...");
							} else {
								price = Double.parseDouble(s);
								checkPrice = false;
							}
						}

						System.out.println("年龄：");
						int age = Integer.parseInt(sc.nextLine());

						System.out.println("性别：");
						String gender = sc.nextLine();
						
						pets[index++] = new String[] {++seq + "",category,kind,price+"",age+"",gender};

						break;
					case "2":
						System.out.println("请输入要修改宠物的id：");
						String delId = sc.nextLine();
						
						for(int i = 0;i<index;i++) {
							if(pets[i][0].equals(delId)) {
								System.arraycopy(pets, i+1, pets, i, index-i-1);
								pets[index-1] = null;
								index--;
								break;
							}
						}
						
						
						break;
					case "3":
						System.out.println("请输入要修改宠物的id：");
						String sId = sc.nextLine();
						
						System.out.println("请输入新的价格：");
						String newPrice = sc.nextLine();
						
						for(int i = 0;i<pets.length;i++) {
							if(pets[i][0].equals(sId)) {
								pets[i][3]=newPrice;
								break;
							}
						}
						break;
					case "4":
						for (int i = 0; i < index; i++) {
							System.out.println(pets[i][0] + "-" + pets[i][1] + "-" + pets[i][2] + "-" + pets[i][3] + "-"
									+ pets[i][4] + "-" + pets[i][5]);
						}
						break;
					case "0":
						isContinue = false;
						break;
					default:
						break;
					}
				}
			} else {
				System.out.println("用户名或密码错误");
				count++;
			}
		}
		System.out.println("对不起，您今天登录出错超过最大限制，请明天再尝试...");

	}

}
